.TH std::span::last 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::span::last \- std::span::last

.SH Synopsis
   template< std::size_t Count >                                      \fB(1)\fP \fI(since C++20)\fP
   constexpr std::span<element_type, Count> last() const;
   constexpr std::span<element_type, std::dynamic_extent> last(       \fB(2)\fP \fI(since C++20)\fP
   size_type Count ) const;

   Obtains a span that is a view over the last Count elements of this span. The program
   is ill-formed if Count > Extent. The behavior is undefined if Count > size().

.SH Return value

   A span r that is a view over the last Count elements of *this, such that r.data() ==
   this->data() + (this->size() - Count) && r.size() == Count.

.SH Example


// Run this code

 #include <iostream>
 #include <span>
 #include <string_view>

 void println(std::string_view const title, auto const& container)
 {
     std::cout << title << '[' << std::size(container) << "]{ ";
     for (auto const& elem : container)
         std::cout << elem << ", ";
     std::cout << "};\\n";
 };

 void run(std::span<const int> span)
 {
     println("span: ", span);

     std::span<const int, 3> span_last = span.last<3>();
     println("span.last<3>(): ", span_last);

     std::span<const int, std::dynamic_extent> span_last_dynamic = span.last(2);
     println("span.last(2): ", span_last_dynamic);
 }

 int main()
 {
     int a[8]{1, 2, 3, 4, 5, 6, 7, 8};
     println("int a", a);
     run(a);
 }

.SH Output:

 int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
 span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
 span.last<3>(): [3]{ 6, 7, 8, };
 span.last\fB(2)\fP: [2]{ 7, 8, };

.SH See also

   first   obtains a subspan consisting of the first N elements of the sequence
           \fI(public member function)\fP
   subspan obtains a subspan
           \fI(public member function)\fP
